
package character_11_;

/**
 *
 * @author Administrator
 */
public class Clerk {
    
    private int product = -1;
    
    public synchronized void setProduct(int product) throws InterruptedException{
        waitIfFull();
        this.product = product;
        System.out.println("生产者设定" + this.product);//"生产者设定" +　
        notify();
    }
    
    private synchronized void waitIfFull() throws InterruptedException {
        while (this.product != -1) {            
            wait();
        }
    }
    public synchronized int getProduct() throws InterruptedException{
    
        waitIfEmpty();
        int p = this.product;
        this.product = -1;
        System.out.println("消费者取走" + p);
        notify();
        return p;
    }
    
    private synchronized void waitIfEmpty() throws InterruptedException{
    
        while (this.product == -1) {            
            wait();
        }
    }
    
}
